﻿using System;

using EFB.Lib.Entity;
using EFB.Lib.Repository;



namespace EFB.Lib.Process {

    public class OperacaoLogProcess {

        private OperacaoLogRepository Repository = null;

        public OperacaoLogProcess() {

            Repository = new OperacaoLogRepository();
        }

        public void Incluir(OperacaoLogEntity OperacaoLog) {
            try
            {

                if (OperacaoLog.Operacao.Equals("Producao", StringComparison.InvariantCultureIgnoreCase))
                {
                    if (String.IsNullOrEmpty(OperacaoLog.Op))
                    {
                        throw new Exception("Informe OP.");
                    }

                    Repository.cadastroProducao(OperacaoLog);
                    Repository.CommitTransaction();
                }
                else
                {
                    if (!String.IsNullOrEmpty(OperacaoLog.Rast))
                    {
                        if (!String.IsNullOrEmpty(OperacaoLog.Qtd))
                        {

                            if (Convert.ToInt32(OperacaoLog.Qtd) > 0)
                            {

                                if (Repository.VerificaExitenciItemIdent_Rastrebilidade(OperacaoLog.Rast))
                                {
                                    if (OperacaoLog.Operacao.Equals("Entrada"))
                                    {
                                        Repository.AtualizarQtdOriginal_ItemIdent_Por_Rastreab(OperacaoLog);

                                    }
                                    else if (OperacaoLog.Operacao.Equals("Saida"))
                                    {
                                        Repository.Saida_ItemIdent(OperacaoLog);
                                    }

                                }
                                else
                                {
                                    Repository.IncluirItemIdentificacao(OperacaoLog);
                                }

                                Repository.Incluir(OperacaoLog);
                                Repository.CommitTransaction();
                            }
                            else
                            {
                                //erro na quantidade
                                throw new Exception("Informe quantidade maior que zero.");
                            }
                        }
                        else
                        {
                            throw new Exception("Informe a quantidade.");
                        }
                    }
                    else
                    {
                        {
                            throw new Exception("Informe a rastreabilidade.");
                        }
                    }
                }
            }
            catch (Exception Except)
            {               
                Repository.RollbackTransaction();
                throw new Exception(Except.Message);
            
            }
            finally {
                Repository.DatabaseDisconnect();
            }
          
            
        }
   
    }

}
